# -*- coding: utf-8 -*-

# Define your item pipelines here
#
# Don't forget to add your pipeline to the ITEM_PIPELINES setting
# See: https://doc.scrapy.org/en/latest/topics/item-pipeline.html
import pymongo
import requests
import os


class FangtianxiaPipeline(object):
    def __init__(self):
        myclient = pymongo.MongoClient("127.0.0.1", 27017)
        if '房天下' in myclient.list_database_names():
            myclient.drop_database('房天下')
        mydb = myclient['房天下']
        self.mycol = mydb['海淀']
        self._id = 1

    def process_item(self, item, spider):
        item['_id'] = self._id
        self.mycol.insert_one(dict(item))
        self._id += 1
        return item


class FangtianxiaPipeline_pic(object):
    def __init__(self):
        os.makedirs('房天下', exist_ok=True)

    def process_item(self, item, spider):
        pic_url = "http:" + item['tu_url']
        respones = requests.get(pic_url)
        with open('房天下/' + pic_url[-28:-12] + '.jpg', 'wb')as f:
            f.write(respones.content)
